<?php

namespace App\Http\Controllers\OpenShop\Feishu;

use App\Enums\Check\CheckEnum;
use App\Enums\OpenShop\InvestmentPromotion\MainProcessEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Events\OpenShop\OpenShopProcessEvent;
use App\Http\Controllers\BaseController;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderLogModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPayHistoryModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignModel;
use App\Remote\Orm\OrmGoodsRemote;
use App\Remote\Orm\OrmNewOrderRemote;
use App\Service\OpenShop\Feishu\DesignerEquipmentService;
use App\Service\OpenShop\Feishu\KfosService;
use App\Service\OpenShop\User\RmsUserService;
use App\Support\ChangeSupport;
use Illuminate\Http\Request;

/**
 * 设计师确认设备
 */
class DesignerEquipmentController extends BaseController
{
    public function index(Request $request, string $orderNo)
    {
        $sysType = $request->get('sysType');
        if (!$sysType) {
            $sysType = CheckEnum::SYS_TYPE_OPEN_SHOP_EQUIPMENT;
        }

        return $this->success(KfosService::equipmentDeliveryNotes($orderNo, $sysType));
    }

    public function detail(Request $request, string $orderNo)
    {
        $sysType = (int)$request->get('sysType');
        if (!$sysType) {
            $sysType = CheckEnum::SYS_TYPE_OPEN_SHOP_EQUIPMENT;
        }
        try {
            $data = DesignerEquipmentService::detail($orderNo, $sysType);
        } catch (\Exception $exception) {
            return $this->fail(400, $exception->getMessage());
        }
        return $this->success($data);
    }

    public function save(Request $request)
    {
        $userInfo = (array)getRmsUserInfoByAuth();
        $this->validate($request, [
            'orderNo'          => 'required|string',
            'equipments'       => 'required|array',
            'equipments.*.id'  => 'required|int|min:1',
            'equipments.*.num' => 'required|int|min:1',
        ]);

        $body = DesignerEquipmentService::equipmentSave($userInfo, $request->orderNo, $request->equipments);

        if (0 !== $body['code']) {
            return $this->fail($body['code'], $body['msg']);
        }

        return $this->success($body['data']);
    }

    /**
     * 设备发货
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function deliveryNoteLogistics(Request $request)
    {
        $this->validate($request, [
            'id'                     => 'required',
            'list'                   => 'required|array',
            'list.*.type'            => 'required|int|in:1,2',
            'list.*.goods_name'      => 'required|string',
            'list.*.consignee'       => 'required|string',
            'list.*.consignee_phone' => 'required|string',
            'list.*.province'        => 'required|string',
            'list.*.city'            => 'required|string',
            'list.*.district'        => 'required|string',
            'list.*.detail_address'  => 'required|string',
        ]);
        $body = OrmNewOrderRemote::deliveryNoteLogistics((int)$request->post('id'), (array)$request->post('list'));
        if (0 !== ($body['code'] ?? null)) {
            return $this->fail(400, $body['msg'] ?? '');
        }
        return $this->success();
    }

    public function orderDetail(Request $request)
    {
        $sysType = (int)$request->get('sysType');
        $shopId  = (int)$request->get('shopId');
        $orderNo  = (string)$request->get('orderNo');
        if (!$sysType) {
            $sysType = CheckEnum::SYS_TYPE_OPEN_SHOP_EQUIPMENT;
        }
        $this->validate($request, [
            'id' => 'numeric'
        ]);
        if (!$shopId && strlen($orderNo)) {
            $shopId = (int)OpenShopMainProcessModel::query()
                ->where('orderNo', $orderNo)
                ->value('shopId');
        }
        $id = $request->get('id', 0);
        return $this->success(DesignerEquipmentService::orderDetail($id, $sysType, $shopId));
    }

    public function saveOrmOrder(Request $request)
    {
        $userInfo = (array)getRmsUserInfoByAuth();
        $this->validate($request, [
            'orderNo'          => 'required|string',
            'id'               => 'numeric',
            'equipments'       => 'required|array',
            'equipments.*.id'  => 'required|int|min:1',
            'equipments.*.num' => 'required|int|min:1',
            'equipments.*.non_standard_unit_num' => 'numeric',
            'equipments.*.non_standard_remark'   => 'string',
        ]);
        $newOrderId = $request->post('id', 0);
        $sysType    = $request->post('sysType');
        if (!$sysType) {
            $sysType = CheckEnum::SYS_TYPE_OPEN_SHOP_EQUIPMENT;
        }

        $res = DesignerEquipmentService::saveOrmOrder($userInfo, $request->orderNo, $request->equipments, $newOrderId, $sysType);
        if (is_array($res)) {
            return $this->fail($res['code'] ?? 0, $res['msg'] ?? '订货系统开小差了');
        }
        return $this->success($res);
    }
}
